/*
 * main implementation: use this sample to create your own application
 *
 */


#include "support_common.h" /* include peripheral declarations and more */
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
/* Standard IO is only possible if Console or UART support is enabled. */
#include <stdio.h>
#endif

#include "dtim.h"

#include "gpio.h"

#include "uc_dipsw.h"

#include "uc_led.h"



static int dipsw_delay_poll(); 
static int dipsw_onoff_poll(); 
static void flash_led_sequence(int p_delay); 
static void init(); 


int main(void)
{
	init(); 
	for(;;)
	{	  
	   	flash_led_sequence(dipsw_delay_poll()*1000);
	}
}



/*
 * Polls DIP switches 3, and 4 to see what 
 * the delay should be set to. 
 * Returns the delay
 */
static int dipsw_delay_poll()
{
	//Poll switches 3 and 4 and returns the DELAY
	int switches = (uc_dipsw_get_state(3)<<1) | uc_dipsw_get_state(4);
	//printf("Switches: %d \n", switches); For debugging
	if(switches == 0)
		return 250; 
	else if(switches == 1)
		return 350; 
	else if(switches == 2)
		return 450; 
	else if(switches == 3)
		return 550; 
	else{ 
		return 1000; //For debugging
	}
}

/*
 * Polls DIP switch 2 to see if the program
 * should be reversed because it has been moved
 * to the Off position or if the program should
 * go back to its original direction because it 
 * has been moved to the On position
 */
static int dipsw_onoff_poll()
{
	//RETURNS whether or not the switch is ON
	return gpio_port_dd_get_pin_state(2);
}

/*
 * Flash the LED's in the proper sequence per the 
 * project requirements.
 */
static void flash_led_sequence(int p_delay)
{
	if(dipsw_onoff_poll())	// Backwards direction
	{
		for(int c=3; c>=0; c--)
		{
			uc_led_on(c); 
			dtim0_delay(p_delay); 
			uc_led_off(c); 
			dtim0_delay(p_delay); 
		}
	}
	else	//	Forward direction
	{
		for(int c=0; c<4; c++)
				{
					uc_led_on(c); 
					dtim0_delay(p_delay); 
					uc_led_off(c); 
					dtim0_delay(p_delay); 
				}
	}
}

/*
 * Calls uc_dipsw_init() to initialize the DIP
 * switch, uc_led_init() to initialize the LED's,
 * and dtim0_init() to initialize DTIM0.
 */
static void init()
{
	uc_dipsw_init(); 
	uc_led_init(); 
	dtim0_init();
	

}


